package com.example.app2;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.example.app2.Login.login;
import com.google.gson.Gson;

import org.jetbrains.annotations.NotNull;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText username;
    private EditText password;
    private CheckBox checkBox;
    private SharedPreferences config;
    private ProgressBar progressBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        username = findViewById(R.id.username);
        password = findViewById(R.id.password);
        Button login = findViewById(R.id.login);
        login.setOnClickListener(this);
        checkBox = findViewById(R.id.checkbox);

        config = getSharedPreferences("config", 0);
        String username2 = config.getString("username", null);
        String password2 = config.getString("password", null);
        if (username2 !=null){
            username.setText(username2);
        }
        if (password !=null){
            password.setText(password2);
        }
        progressBar = findViewById(R.id.progress_circular);
    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        inputMethodManager.hideSoftInputFromWindow(username.getWindowToken(),0);
        return super.onTouchEvent(event);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.login:
                progressBar.setVisibility(View.VISIBLE);
                OkHttpClient okHttpClient = new OkHttpClient();
                JSONObject jsonObject = new JSONObject();
                try {
                    jsonObject.put("username",username.getText());
                    jsonObject.put("password",password.getText());
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                RequestBody requestBody = RequestBody.create(jsonObject.toString(), MediaType.parse("application/json"));
                Request request = new Request.Builder().url("https://mock.w2code.com/api/login").post(requestBody).build();
                okHttpClient.newCall(request).enqueue(new Callback() {
                    @Override
                    public void onFailure(@NotNull Call call, @NotNull IOException e) {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(MainActivity.this,"9999",Toast.LENGTH_SHORT).show();
                            }
                        });
                    }

                    @Override
                    public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                        String string = response.body().string();
                        login login = new Gson().fromJson(string, login.class);
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                //登录成功
                                    if(login.data.verifySuccess){
                                        progressBar.setIndeterminate(true);
                                        progressBar.setProgress(100);
                                        //保存密码
                                        if(checkBox.isChecked()){
                                            SharedPreferences.Editor edit = config.edit();
                                            edit.putString("username", String.valueOf(username.getText()));
                                            edit.putString("password", String.valueOf(password.getText()));
                                            edit.commit();
                                        }
                                        //弹出提示语
                                        Toast.makeText(MainActivity.this,login.data.userInfo.username+",欢迎你",Toast.LENGTH_SHORT).show();
                                        startActivity(new Intent(MainActivity.this,MainActivity2.class));
                                    }else {
                                        //登录失败
                                        Toast.makeText(MainActivity.this,"密码输入错误",Toast.LENGTH_SHORT).show();
                                        password.setText("");
                                    }

                            }
                        });

                    }
                });
                progressBar.setVisibility(View.GONE);
                break;
        }
    }
}